package com.cal.utms.model.maintenance;

import com.cal.utms.dto.maintenance.InstrumentTypeDto;
import com.cal.utms.entity.InstrumentType;
import com.cal.utms.enumType.InterestRate;
import com.cal.utms.enumType.InterestType;
import com.cal.utms.enumType.WithholdingTax;

public class InstrumentTypeModel {
	
	public InstrumentType createEntity(InstrumentTypeDto instrumentTypeDto) {
		InstrumentType instrumentType = new InstrumentType();
		return mapDtoInsType(instrumentTypeDto, instrumentType);
	}
	
	public InstrumentType updateEntity(InstrumentTypeDto insTypeDto, InstrumentType insType){
		return mapDtoInsType(insTypeDto, insType);		
	}
	
	public InstrumentType mapDtoInsType(InstrumentTypeDto instrumentTypeDto, InstrumentType instrumentType){
		instrumentType.setInstrumentName(instrumentTypeDto.getInstrumentName());
		instrumentType.setInstrumentType(instrumentTypeDto.getIstrumentType()==1?com.cal.utms.enumType.InstrumentType.G:com.cal.utms.enumType.InstrumentType.O);
		instrumentType.setScripts((byte)(instrumentTypeDto.getScripts()==1?1:0));
		instrumentType.setDenominals((byte)(instrumentTypeDto.getDenominals()==1?1:0));
		instrumentType.setListed((byte)(instrumentTypeDto.getListed()==1?1:0));
		instrumentType.setShortMaturable((byte)(instrumentTypeDto.getShortMaturable()==1?1:0));
		instrumentType.setCalculationInput(instrumentTypeDto.getCalculationInput());
		WithholdingTax wht;
		if(instrumentTypeDto.getWithholdingTax().equals("No")){
			wht = WithholdingTax.N;
		}else if(instrumentTypeDto.getWithholdingTax().equals("Upfront")){
			wht = WithholdingTax.U;
		}else if(instrumentTypeDto.getWithholdingTax().equals("Maturity")){
			wht = WithholdingTax.M;
		}else{
			wht = WithholdingTax.V;
		}
		instrumentType.setWithholdingTax(wht);
		instrumentType.setInterestRate(instrumentTypeDto.getInterestRate().equals("Fixed")?InterestRate.F:InterestRate.V);
		instrumentType.setInterestType(instrumentTypeDto.getInterestType().equals("Straight")?InterestType.S:InterestType.D);
		instrumentType.setDayBasis(instrumentTypeDto.getDayBasis());
		instrumentType.setCouponAvailability((byte)instrumentTypeDto.getCouponAvailability());
		instrumentType.setCuponFrequency(instrumentTypeDto.getCuponFrequency());
		return instrumentType;		
	}
	
	public InstrumentTypeDto decodeEntity(InstrumentType instrumentType) {
		InstrumentTypeDto instrumentTypeDto = new InstrumentTypeDto();
		instrumentTypeDto.setId(instrumentType.getId());
		instrumentTypeDto.setInstrumentName(instrumentType.getInstrumentName());
		instrumentTypeDto.setIstrumentType((byte)(instrumentType.getIstrumentType()==com.cal.utms.enumType.InstrumentType.G?1:0));
		instrumentTypeDto.setScripts(instrumentType.getScripts());
		instrumentTypeDto.setDenominals(instrumentType.getDenominals());
		instrumentTypeDto.setListed(instrumentType.getListed());
		instrumentTypeDto.setShortMaturable(instrumentType.getShortMaturable());
		if(instrumentType.getWithholdingTax()==WithholdingTax.N){
			instrumentTypeDto.setWithholdingTax("No");
		}else if(instrumentType.getWithholdingTax()==WithholdingTax.U){
			instrumentTypeDto.setWithholdingTax("Upfront");
		}else if(instrumentType.getWithholdingTax()==WithholdingTax.M){
			instrumentTypeDto.setWithholdingTax("Maturity");			
		}else{
			instrumentTypeDto.setWithholdingTax("Vary");
		}		
		instrumentTypeDto.setInterestRate(instrumentType.getInterestRate()==InterestRate.F?"Fixed":"Floating");
		instrumentTypeDto.setInterestType(instrumentType.getInterestType()==InterestType.S?"Straight":"Discounted");
		instrumentTypeDto.setDayBasis(instrumentType.getDayBasis());
		instrumentTypeDto.setCouponAvailability(instrumentType.getCouponAvailability());
		instrumentTypeDto.setCuponFrequency(instrumentType.getCuponFrequency());
		instrumentTypeDto.setCalculationInput(instrumentType.getCalculationInput());
		return instrumentTypeDto;
	}
}
